class Calendar {
    constructor(year, month) {
        const today = new Date()
        this.year = year || today.getFullYear()
        this.month = month || (today.getMonth()+1)
        this.monthDayArray = []
        this.init()
    }
    getMonthDay(year, month){
        return new Date(year, month+1, 0).getDate()
    }
    getPrevMonthLastDays(year, month, count){
        if (month === 1) {
            year = year - 1
            month = 12
        } else {
            month = month - 1
        }
        const dayCount = this.getMonthDay(year, month)
        const arr = []
        for(let i=dayCount; i>dayCount-count; i--) {
            arr.unshift({
                date: i,
                year,
                month,
                dateStr: i < 10 ? `0${i}` : i,
                monthStr: month < 10 ? `0${month}` : month,
                active: false,
            })
        }
        return arr
    }
    getNextMonthLastDays(year, month, count){
        if (month === 12) {
            year = year + 1
            month = 1
        } else {
            month = month + 1
        }
        const arr = []
        for(let i=1; i<=count; i++) {
            arr.push({
                date: i,
                year,
                month,
                dateStr: i < 10 ? `0${i}` : i,
                monthStr: month < 10 ? `0${month}` : month,
                active: false,
            })
        }
        return arr
    }
    getMonthDayArray(year, month) {
        let dayArray = []
        const dayCount = this.getMonthDay(year, month)
        // [日，一，二，三，四，五，六]
        const firstDayWeekIndex = new Date(`${year}-${month}-1`).getDay()
        dayArray = this.getPrevMonthLastDays(year, month, firstDayWeekIndex)
        for(let i=1; i<= dayCount; i++) {
            dayArray.push({
                date: i,
                year,
                month,
                dateStr: i < 10 ? `0${i}` : i,
                monthStr: month < 10 ? `0${month}` : month,
                active: false,
            })
        }
        const count = 7 - dayArray.length % 7
        dayArray = dayArray.concat(this.getNextMonthLastDays(year, month, count))
        this.monthDayArray = dayArray
        return this.monthDayArray
    }
    prevMonth(){
        if (this.month === 1) {
            this.month = 12
            this.year -= 1
        } else {
            this.month -= 1
        }
        this.getMonthDayArray(this.year, this.month)
    }
    nextMonth(){
        if (this.month === 12) {
            this.month = 1
            this.year += 1
        } else {
            this.month += 1
        }
        this.getMonthDayArray(this.year, this.month)
    }
    prevYear(){
        this.year -= 1
        this.getMonthDayArray(this.year, this.month)
    }
    nextYear(){
        this.year += 1
        this.getMonthDayArray(this.year, this.month)
    }
    changeYear(year){
        this.year = year
        this.getMonthDayArray(this.year, this.month)
    }
    changeMonth(month){
        this.month = month
        this.getMonthDayArray(this.year, this.month)
    }
    init(){
        this.getMonthDayArray(this.year, this.month)
    }
}
export default Calendar
